package com.xzit.rcpyxt.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import com.xzit.rcpyxt.dto.ResultModel;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.alibaba.fastjson.JSONObject;


public class HttpCommon {
	
	/** 请求头 */
	public static final String contentType = "application/json;charset=UTF-8";
	public static final String contentType_text = "text/html;charset=UTF-8";
	public static final String contentType_form = "application/x-www-form-urlencoded";
	public static final int connTime = 1*60*1000;//连接超时
	public static final int timeOut = 3*60*1000;//
	public static final int soTimeOut = 1*60*1000;//读数据超时
	
	//创建连接管理对象
	private static MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
	
	static {
		//设置连接参数
		if (manager == null) {
			manager = new MultiThreadedHttpConnectionManager();
		}
		HttpConnectionManagerParams params = new HttpConnectionManagerParams();
		params.setMaxTotalConnections(1000);// 最大连接数
		params.setConnectionTimeout(connTime);// 连接超时
		params.setSoTimeout(soTimeOut); // 读数据超时
		manager.setParams(params);
	}
	
	private static HttpClient httpClient;
	
	public static HttpClient getHttpClient() {
		httpClient = new HttpClient(manager);
        // 字符集
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, contentType);
		return httpClient;
	}

	public static synchronized ResultModel get(String url) throws HttpException, IOException{
		GetMethod get=new GetMethod(url);
		int state=getHttpClient().executeMethod(get);
		return new ResultModel(state,  new String(get.getResponseBody(), "UTF-8"));
	}

	/**
	 * get请求
	 */
	public static synchronized JSONObject getTypeHttp(String urlPath){
		try {
			URL url = new URL(urlPath);    // 把字符串转换为URL请求地址  
			URLConnection connection = url.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 
            connection.setConnectTimeout(connTime);
            connection.setReadTimeout(timeOut);
            // 获取输入流  
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));  
            String line;  
            StringBuilder sb = new StringBuilder();  
            while ((line = br.readLine()) != null) {// 循环读取流  
                sb.append(line);  
            }  
            br.close();// 关闭流  
            return JSONObject.parseObject(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}